package com.baiytfp.hf.HFDataExchange.action.controller.service;

import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.common.util.StringUtil;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.hfutils.*;
import com.baiytfp.hf.HFDataExchange.action.service.UserService;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Config;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_account;
import com.baiytfp.hf.HFDataExchange.common.utils.CommonUtil;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/service/admin")
public class AdminSController extends BaseController{
	
	@Autowired
	UserService userService;
	
	// 修改密码
	@RequestMapping(value = "/modifypassword", method= RequestMethod.POST)
	public ModelAndView modifypassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String usercode = StringUtil.getString(HFWeb.getIstance().getUserCode(),"");
		String prepassword = HFRequest.getIstance().getString(request, "prepassword","");
		String password = HFRequest.getIstance().getString(request, "password","");
		int res = -1;
		if (usercode.equals("")==false && prepassword.equals("")==false && password.equals("")==false) {
			User_account account = userService.getUserByUserCode(usercode);
			if (account!=null) {
				if (HFCodec.getIstance().encoderToMD5(prepassword).equals(account.getUser_password())) {
					
					//判断密码
					if(!CommonUtil.matches(password,account.getUser_code())) {
						HFResponse.getIstance().returnResult(response, -1, "密码须含有至少1个以上的数字、字母、特殊符号，长度大于等于8，且不包含帐号", null);
						return null;
					}
					
					account.setUser_password(HFCodec.getIstance().encoderToMD5(password));
					res = (int) userService.udateUser(account);
				} else {
					res = -2;
				}
			}
		}
		HFResponse.getIstance().returnResult(response, res, res==1?"success":"fail", null);
		return null;
	}
	
	@RequestMapping("/count")
	public ModelAndView countData(HttpServletRequest request ,HttpServletResponse response) throws Exception {
		String type = HFRequest.getIstance().getString(request, "type","-1");
		HFPager hfPager = new HFPager(request);
		Map map = new HashMap<>(16);
		map.put("type", type);
		hfPager.setParams(map);
		
		StringBuffer sql = new StringBuffer();
		sql.append(" select a.* , b.type_name , c.user_name from ")
		.append(TableName.LOG_RECORD).append(" a ")
		.append("  left join ").append(TableName.LOG_TYPE).append(" b  on a.type_id = b.id ")
		.append("  left join ").append(TableName.USER_INFO).append(" c  on a.create_man = c.user_code ")
		.append(" where type_id like ? ");
		HFDBHelper.list_split(sql.toString(), Map.class, hfPager, new Object[] {type});
		HFResponse.getIstance().returnResult(response, hfPager.getResult());
		return null;
	}
	
	
	
	

	// 不返回任何视图页面
	@RequestMapping("/sessionlong")
	public ModelAndView sessionlong(HttpServletRequest request) throws Exception {
		// 主要用来长期保持登陆状态
		return null;
	}

	// 配置页
	//@RequiresRoles("admin")
	@RequiresPermissions("admin:setconfig")
	@RequestMapping(value="/setconfig", method= RequestMethod.POST)
	public ModelAndView setconfig(HttpServletRequest request,
								  HttpServletResponse response) throws Exception {



		List<Config> list = new ArrayList();
		Config configVO = null;

		for (int i = 0; i < HFRequest.getIstance().getInt(request, "num", 0); i++) {
			configVO = new Config();
			configVO.setType(0);
			configVO.setThe_key(HFRequest.getIstance().getString(request, "key_" + i, ""));
			configVO.setThe_value(HFRequest.getIstance().getString(request, "val_" + i, ""));
			configVO.setThe_desc(HFRequest.getIstance().getString(request, "desc_" + i, ""));
			list.add(configVO);
		}

		for (int i = 0; i < HFRequest.getIstance().getInt(request, "c_num", 0); i++) {
			if (!"".equals(HFRequest.getIstance().getString(request, "c_key_" + i, ""))
					&& !"".equals(HFRequest.getIstance().getString(request, "c_name_"
					+ i, ""))) {
				configVO = new Config();
				configVO.setType(HFRequest.getIstance().getInt(request, "c_type_" + i, 1));
				configVO.setThe_key(HFRequest.getIstance().getString(request, "c_key_" + i,
						""));
				configVO.setThe_value(HFRequest.getIstance().getString(request, "c_value_"
						+ i, ""));
				configVO.setThe_desc(HFRequest.getIstance().getString(request, "c_name_"
						+ i, ""));
				list.add(configVO);
			}
		}

		//更新
		for(Config cfVO : list){
			long rownum = HFDBHelper.rownum("", "select * from " + TableName.CONFIG + " c where the_key=? ", cfVO.getThe_key());
			if(rownum > 0){
				HFDBHelper.update(TableName.CONFIG, " where the_key=? ", cfVO, cfVO.getThe_key());
			}else{
				HFDBHelper.add(TableName.CONFIG, cfVO);
			}
		}

		HFResponse.getIstance().returnSuccess(response);

		return null;
	}

	// 删除一个配置
	@RequestMapping(value="/delconfig", method= RequestMethod.POST)
	public ModelAndView delconfig(HttpServletRequest request,
								  HttpServletResponse response) throws Exception {

		String key = HFRequest.getIstance().getString(request, "key");

		HFResponse.getIstance().returnResultByRes(response, HFDBHelper.delete(TableName.CONFIG, " where the_key=? ", key));

		return null;
	}

	// 刷新配置缓存
	@RequestMapping("/refresh_config_cache")
	public ModelAndView refresh_config_cache(HttpServletRequest request, HttpServletResponse response) throws Exception {

		HFCache.getIstance().delete("config__" + 0);
		HFCache.getIstance().delete("config_" + 1 + "_" + 0);
		HFCache.getIstance().delete("config_" + 2 + "_" + 0);
		HFCache.getIstance().delete("config__");

		HFResponse.getIstance().returnSuccess(response);

		return null;
	}

}
